﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CardLogistics.Web.Components;
using System.Text;

namespace CardLogistics.Web.MainPage.Purchase.Contract
{
    public partial class ListInfo : BasePage
    {
        private static readonly CardLogistics.DAL.Purchase.Purchase_ContractData dal = new DAL.Purchase.Purchase_ContractData();
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserControl_ToolBar1.DeleteEvent += new EventHandler(WebUserControl_ToolBar1_DeleteEvent);
            if (!IsPostBack)
            { 
                //状态 0 未提交 1 待审核 2 审核中 3 驳回 4 审核不通过 5审核通过
                BindDLL(ddlIsSubmit);
                BindDLL(ddltypename, "BASIC_BASICTYPE", "TYPENAME", "ID", " and TYPENO=1 and STATUS=0 and ISDELETE=0");  //采购类型
                BindDLL(ddlsupName, "basic_suppliers", "suppliersname", "suppliersid", " and STATUS=0 and ISDELETE=0");  //供应商
                BindRpt(LoadStrWhere);
            }
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void WebUserControl_ToolBar1_DeleteEvent(object sender, EventArgs e)
        {
            string delId = hdf_Id.Value;
            if (!string.IsNullOrEmpty(delId))
            {
                string[] ids = delId.TrimEnd(',').Split(',');
                int resCount = 0;
                for (int i = 0; i < ids.Length; i++)
                {
                    string resStr = dal.Delete(ids[i]);
                    if (resStr.Split(',')[0] == "ok")
                    {
                        resCount++;
                        InsertLog("删除采购合同");
                    }
                }
                BindRpt("");
            }
        }

        /// <summary>
        /// 绑定Rpt数据
        /// </summary>
        private void BindRpt(string strWhere)
        {
            int recordCount = 0;
            int pageCount = 0;
            StringBuilder sbStr = new StringBuilder();
            sbStr.Append(" 1=1 ");
            if (txt_CONNO.Text.Trim().Length > 0)
                sbStr.Append(" and CONNO like '%" + txt_CONNO.Text.Trim().ToUpper() + "%' ");//合同编号
            if (txt_billno.Text.Trim().Length > 0)
                sbStr.Append(" and billno like '%" + txt_billno.Text.Trim().ToUpper() + "%' ");//需求单编号
            if (ddltypename.SelectedIndex != 0)
                sbStr.Append(" and buytypeId='" + ddltypename.Value + "'");//采购类型
            if (ddlsupName.SelectedIndex != 0)
                sbStr.Append(" and SuppId='" + ddlsupName.Value + "'");//供应商
            if (txt_StartDate.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(SIGNDATE,'yyyy-MM-dd')>='" + txt_StartDate.Text.Trim() + "'");//签定开始日期
            if (txt_EndDate.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(SIGNDATE,'yyyy-MM-dd')<='" + txt_EndDate.Text.Trim() + "'");//签定结束日期
            //审核状态
            if (ddlIsSubmit.SelectedValue != "-1")
            {
                sbStr.Append(" and IsSubmit=" + ddlIsSubmit.SelectedValue);
            }
            sbStr.Append(strWhere);
            DataTable dt = CardLogistics.DAL.CommonData.QuickPage_Oracle("v_purchase_contract", "ID", " issubmit,signdate desc,conno desc", WebPageSize, WebPageIndex, sbStr.ToString(), "*", out pageCount, out recordCount);
            SetNetPagerHtml(AspNetPager1, dt, rptList, recordCount);

            #region 导出数据
            //导出语句
            string strExpSql = "select CONNO 合同编号,billno 需求单编号,CONNAME 合同名称,typename 采购类型,suppliersname 供应商名称,SIGNDATE 签定日期,BEDATE 生效日期,DELIVERYDAYS 供货期天数,deTotal 卡片数量,SIGNADDRESS 签定地点,CONPIC 负责人,REMARK 备注 from v_purchase_contract where" + sbStr.ToString();
            if (!Page.IsClientScriptBlockRegistered("regExportSql"))
                Page.RegisterClientScriptBlock("regExportSql", "<script language=\"Javascript\">var qsql=\"" + Server.UrlEncode(strExpSql) + "\";</script>");
            #endregion
        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            WebPageIndex = AspNetPager1.CurrentPageIndex;
            BindRpt("");
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            BindRpt("");
        }
    }
}